package role

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSaveRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveRoleLogic {
	return &SaveRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SaveRoleLogic) SaveRole(req *types.SaveRoleReq) (resp *types.SaveRoleResp, err error) {
	// 1.builder rpc request
	saveRoleReq := pb.SaveRoleReq{RoleName: req.RoleName, RoleRemark: req.RoleRemark, DeptIdArr: req.DeptIdArr,
		DeptId: req.DeptId, MenuIdArr: req.MenuIdList}
	// 2.request rpc
	saveRole, err := l.svcCtx.UserCenterClient.SaveRole(l.ctx, &saveRoleReq)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	return &types.SaveRoleResp{RoleId: saveRole.RoleId}, err
}
